package com.dtagri.fjwlw.ui;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.dtagri.fjwlw.R;
import com.dtagri.fjwlw.base.BaseSimpleActivity;
import com.dtagri.fjwlw.broadcastreceiver.TReceiver;
import com.dtagri.fjwlw.fragment.VideoCenterFragment;
import com.dtagri.fjwlw.util.TDevice;
import com.dtagri.fjwlw.util.TDVideoHelper.MyHandler;

/**
 * 视频中心
 * 
 * @author wxjn
 * 
 */
public class VideoCenterActivity extends BaseSimpleActivity {
	private View back;
	public static MymsgHandler mymsgHandler;
	@Override
	public void initView() {
		super.initView();
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setActionBarTitle("视频中心");
		// 特别处理返回事件
		back = mActionBar.getCustomView().findViewById(R.id.btn_back);
		back.setOnClickListener(this);
		Looper looper=getMainLooper();
		mymsgHandler=new MymsgHandler(looper);


	}

	@Override
	public void onClick(View arg0) {
		super.onClick(arg0);
		if(arg0==back){
			TReceiver.sendBroadCast(this, TReceiver.CONTROL_BACK);
			TDevice.hideSoftKeyboard(getCurrentFocus());
			onBackPressed();
		}

	}
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
		if(VideoCenterFragment.state==1){
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		}else if(VideoCenterFragment.state==0){
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		}
	}
	@Override
	protected Fragment getFragment() {
		VideoCenterFragment fragment = new VideoCenterFragment();
		fragment.setBaseSimpleActivity((BaseSimpleActivity) this);
		return fragment;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			TReceiver.sendBroadCast(this, TReceiver.CONTROL_BACK);
			break;
		default:
			break;
		}

		return super.onKeyDown(keyCode, event);
	}
	public class MymsgHandler extends Handler{
		public MymsgHandler (Looper looper){
			super(looper);
		}
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			if(msg.arg1==77){	
				setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
			}else if(msg.arg1==78){
				setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
			}
		}
	}
}
